/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.projektjava2;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLNonTransientConnectionException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 * @author Lukáš Slovák
 */
public class CarDAOImplTest {
    
    private EntityManagerFactory emFactory;
    private EntityManager em;
    
    public CarDAOImplTest() {
    }
    
    @BeforeClass
    public static void setUpClass() {
        try {
            //logger.info("Starting in-memory database for unit tests");
            System.out.println("Naštartovanie in-memory databázy");
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            DriverManager.getConnection("jdbc:derby:memory:unit-testing-jpa;create=true").close();
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("Exception during database startup.");
        }
        System.out.println("Uspech");
    }
    
    @AfterClass
    public static void tearDownClass() throws SQLNonTransientConnectionException, SQLException {
        System.out.println("Zastavenie in-memory databázy");
        try {
            DriverManager.getConnection("jdbc:derby:memory:unit-testing-jpa;shutdown=true").close();
        } catch (SQLNonTransientConnectionException ex) {
            if (ex.getErrorCode() != 45000) {
                throw ex;
            }
            // Shutdown success
        }
        //VFMemoryStorageFactory.purgeDatabase(new File("unit-testing-jpa").getCanonicalPath());
        System.out.println("Uspech");
    }
    @Before
    public void setUp() {
        try {
            //logger.info("Building JPA EntityManager for unit tests");
            emFactory = Persistence.createEntityManagerFactory("TestPU");
            em = emFactory.createEntityManager();
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("Exception during JPA EntityManager instanciation.");
        }
        System.out.println("Uspech");
        
        
//        Car car2 = new Car();
//        car2.setAvailable(1);
//        car2.setColour("modra");
//        car2.setNumberOfSeats(5);
//        car2.setType("combi");
//        
//        em.getTransaction().begin();
//        em.persist(car2);
//        em.getTransaction().commit();
    }
    
    @After
    public void tearDown() {
        if (em != null) {
            em.close();
        }
        if (emFactory != null) {
            emFactory.close();
        }
    }
    
    /**
     * Test of find method, of class CarDAOImpl.
     */
    @Test
    public void testFind() {

    }

    /**
     * Test of createCar method, of class CarDAOImpl.
     */
    @Test
    public void testCreateCar() {
//        Car car1 = new Car();
//        car1.setAvailable(1);
//        car1.setColour("cervena");
//        car1.setNumberOfSeats(5);
//        car1.setType("sedan");
//        
//        CarDAOImpl tested = new  CarDAOImpl();
//        tested.createCar(car1);
//        
//        em.getTransaction().begin();
//        em.refresh(car1);
//        Car car2 = em.find(Car.class, car1.getId());
//        em.getTransaction().commit();
//        assertTrue(car1.equals(car2));
    }

    /**
     * Test of remove method, of class CarDAOImpl.
     */
    @Test
    public void testRemove() {

    }

    /**
     * Test of update method, of class CarDAOImpl.
     */
    @Test
    public void testUpdate() {

    }    

    /**
     * Test of getAllCars method, of class CarDAOImpl.
     */
    @Test
    public void testGetAllCars() {

    }

    /**
     * Test of getAllAvailableCars method, of class CarDAOImpl.
     */
    @Test
    public void testGetAllAvailableCars() {

    }

    /**
     * Test of getCar method, of class CarDAOImpl.
     */
    @Test
    public void testGetCar() {

    }
}
